package cn.jb698.codec.msm.zcodec.demo.msm;

import java.util.ArrayList;
import java.util.List;

import cn.jb698.codec.msm.generator.v5base.BcrudCfgObjCreateService;
import cn.jb698.codec.msm.generator.v5base.BcrudCfgObjCreateServiceMysqlImpl;
import cn.jb698.codec.msm.generator.v5base.BcrudCreateCodeFilesImpl;
import cn.jb698.codec.msm.generator.v5base.beans.BcrudCfgTableObj;
import cn.jb698.codec.msm.generator.v5base.beans.BcrudConfigBean;
import cn.jb698.codec.msm.generator.v5base.beans.FileOutputTpltCfg;
import cn.jb698.codec.msm.util.CodecMsmFileOutputTpltUtils;
import cn.jb698.codec.msm.zcodec.demo.CodecPropertiesUtils;

public class ZCodecMysqlDemoBcrudServiceTk {

	public static void main(String[] args) throws Exception {
		
	    BcrudConfigBean configBean = init_bcrudConfigBean();

	    // 构建实现类
        BcrudCfgObjCreateService bcrudCfgObjCreateService = new BcrudCfgObjCreateServiceMysqlImpl();
        List<BcrudCfgTableObj> cfgTableList = bcrudCfgObjCreateService.doBcrudCfgTableObjListCreate(configBean);

        {
            // 二次修改表及字段配置数据,覆盖默认生成的配置信息
        }
        // 最终配置输出到Json文件
        bcrudCfgObjCreateService.doCfgTableObjListOutputToJsonFiles(configBean,cfgTableList); // 输出表信息JSON到配置的输出目录

        // 输出创建的代码
        for(BcrudCfgTableObj tableJsBean : cfgTableList ){
            BcrudCreateCodeFilesImpl bcrudCreateCodeFile = new BcrudCreateCodeFilesImpl();

            // 初始化Baomidou配套模板
            List<FileOutputTpltCfg> fileTpltList4Bmd = CodecMsmFileOutputTpltUtils.doFileOutputTpltCfgAppend4Bmd(configBean, configBean.getFileOutputTpltCfgList());
            configBean.setFileOutputTpltCfgList(fileTpltList4Bmd);
            
            // 输出模板 confBean.getProjRootDir()
            bcrudCreateCodeFile.doCodeFilesCreateAndOutput(configBean, tableJsBean); 
        }
	}

	/**
	 * 初始化库.
	 */
	private static BcrudConfigBean init_bcrudConfigBean() {

        String default_driverClassName = CodecPropertiesUtils.getPropVal("spring.datasource_mysql.driver-class-name"); 
        String default_connectUrl = CodecPropertiesUtils.getPropVal("spring.datasource_mysql.url");
        String default_userName = CodecPropertiesUtils.getPropVal("spring.datasource_mysql.username");
        String default_password = CodecPropertiesUtils.getPropVal("spring.datasource_mysql.password");

        String basePackageName = "cn.jb698.codec.demo.bcrud.dbmysql";
        String xmlMapperPackage = "mybatis.codec.dbmysql.mapper";

        String tgtProjDir = "D:/Develope/workspace/codec/codec_msm_demo"; // 目标项目路径.
        String jsonFile_outDir = tgtProjDir + "/table_jsons/dbmysql";
		String codeOutDir = tgtProjDir + "/src/main/java/";
		String resOutDir = tgtProjDir + "/src/main/resources/";
		String pageOutDir = tgtProjDir + "/src/main/webapp/WEB-INF/jsp/temp_aceadmin/bcrud/";

		List<BcrudCfgTableObj> lmtCfgTables = new ArrayList<>();

		{
			BcrudCfgTableObj cfgTableBean = new BcrudCfgTableObj("mdt_demo_data_inf", "t");
//        mdt_demo_data_inf.setModelJavaTypePackage("cn.jb698.codec.demo.bcrud.dbmysql.entity"); 
			lmtCfgTables.add(cfgTableBean.doHasSwaggerUi(true).doHasPersistence(true).doHasBaomidou(true)); // DEMO数据信息 
		}
		{
			BcrudCfgTableObj cfgTableBean = new BcrudCfgTableObj("mdt_demo_data_ext", "t");
			cfgTableBean.setIgnoreColumns(new String[] {"ext_inf_time"}); // 忽略(淘汰)字段 
			cfgTableBean.setHasSwaggerUi(true); // 添加swaggerui标签
			cfgTableBean.doHasBaomidou(true); // mybatis-plus 支持
			cfgTableBean.setHasAutoCutLengh(true); // 是否自动截取字符串
			lmtCfgTables.add(cfgTableBean); // DEMO数据信息扩展
		}

		BcrudConfigBean confBean = new BcrudConfigBean();
		confBean.setDbDriverClassName(default_driverClassName);
		confBean.setDbConnectionUrl(default_connectUrl);
		confBean.setDbUserName(default_userName);
		confBean.setDbPassword(default_password);

		confBean.setBasePackageName(basePackageName);
		confBean.setJavaCodeRootDir(codeOutDir);
		confBean.setResCodeRootDir(resOutDir);
		confBean.setMapperXmlPackageName(xmlMapperPackage);
		confBean.setPageCodeRootDir(pageOutDir);
		confBean.setJsonFileOutDir(jsonFile_outDir);
		confBean.setCfgTables(lmtCfgTables);
		confBean.setProjRootDir(tgtProjDir);
		
		confBean.setFileOutputTpltCfgList(CodecMsmFileOutputTpltUtils.doFileOutputTpltCfgAppend4Tk(confBean, confBean.getFileOutputTpltCfgList()));
		
		return confBean;

	}

}
